package com.test.dababy.dababy_05_process;

import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.test.dababy.Othe_dababyrsInfo;
import com.test.dababy.R;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

public class Bin_dababydAccount_dababyDialog_Fr extends Dialog {

    private final String page_dababy;

    private final BindAcco_dababyuntLis_dababytener_Fr bindAccountListener_dababy;

    private final Acco_dababyuntCha_dababynnelViewAdapter accountChannelViewAdapter_dababy;


    private float oEwE=0.96615314f;
    private double s7jj=0.7410741007412397;
    private String jrAy="";
    private boolean kCXA=true;
    private double mPvD=0.8345363937401603;
    private String gq06="";
    private String p2jK="";

    public void nFTn4(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getP2jK())) setGq06(getJrAy()+getmPvD()+iskCXA()+getS7jj()+getoEwE());
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setoEwE(float oEwE) {
        this.oEwE = oEwE;
    }

    public void setS7jj(double s7jj) {
        this.s7jj = s7jj;
    }

    public void setJrAy(String jrAy) {
        this.jrAy = jrAy;
    }

    public void setkCXA(boolean kCXA) {
        this.kCXA = kCXA;
    }

    public void setmPvD(double mPvD) {
        this.mPvD = mPvD;
    }

    public void setGq06(String gq06) {
        this.gq06 = gq06;
    }

    public void setP2jK(String p2jK) {
        this.p2jK = p2jK;
    }

    public float getoEwE() {
        return oEwE;
    }

    public double getS7jj() {
        return s7jj;
    }

    public String getJrAy() {
        return jrAy;
    }

    public boolean iskCXA() {
        return kCXA;
    }

    public double getmPvD() {
        return mPvD;
    }

    public String getGq06() {
        return gq06;
    }

    public String getP2jK() {
        return p2jK;
    }

    public Bin_dababydAccount_dababyDialog_Fr(
            @NonNull FragmentActivity activity,
            String page,
            BindAcco_dababyuntLis_dababytener_Fr bindAccountListener) {
        super(activity);
        this.page_dababy = page;
        this.bindAccountListener_dababy = bindAccountListener;

        accountChannelViewAdapter_dababy = new Acco_dababyuntCha_dababynnelViewAdapter(App_dababyShow_dababyCustomize.getInstance_dababy().getAccountChannels_dababy(),page);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dababy_bindaccount_fr);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        findViewById(R.id.dbfa_view_out).setOnClickListener(v -> dismiss());
        findViewById(R.id.dbfa_view_in).setOnTouchListener((v, event) -> true);

        initView_dababy();
    }

    @SuppressLint("SetTextI18n")
    private void initView_dababy(){

        //初始化渠道选择器
        RecyclerView channelRv = findViewById(R.id.dbfa_rv_channel);
        channelRv.setAdapter(accountChannelViewAdapter_dababy);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);//网格布局管理器
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        channelRv.setLayoutManager(gridLayoutManager);

        nFTn4();

        //绑卡输入
        TextView areacode1Tv = findViewById(R.id.dbfa_tv_mobile1_areacode);
        TextView areacode2Tv = findViewById(R.id.dbfa_tv_mobile2_areacode);
        EditText mobile1Et = findViewById(R.id.dbfa_et_mobile1);
        EditText mobile2Et = findViewById(R.id.dbfa_et_mobile2);
        View mobile1View = findViewById(R.id.dbfa_view_mobile_1);
        View mobile2View = findViewById(R.id.dbfa_view_mobile_2);

        mobile1Et.setOnFocusChangeListener((v, hasFocus) ->
                OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"DIALOG_PHONE_INPUT",hasFocus ? "":mobile1Et.getText().toString().trim())
        );

        mobile2Et.setOnFocusChangeListener((v, hasFocus) ->
                OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"DIALOG_PHONE_CONFIRM_INPUT",hasFocus ? "":mobile2Et.getText().toString().trim())
        );

        areacode1Tv.setText(DababyUtil.getAreaCode_dababy());
        areacode2Tv.setText(DababyUtil.getAreaCode_dababy());

        //绑卡提交
        findViewById(R.id.dbfa_btn_add).setOnClickListener(v -> {

            OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"DIALOG_SAVE_CLICK","");

            String mobile1 = mobile1Et.getText().toString().trim();
            String mobile2 = mobile2Et.getText().toString().trim();

            if (TextUtils.isEmpty(mobile1)) mobile1 = "";
            if (TextUtils.isEmpty(mobile2)) mobile2 = "";

            AccountCha_dababynn_dababyel_Fr selectChannel = accountChannelViewAdapter_dababy.getSelectChannel_dababy();
            String channel = selectChannel==null ? "":selectChannel.getValue_dababy();

            Othe_dababyrsInfo.getInstance_dababy().setFrontChannel(channel);

            String finalMobile = mobile1;

            startLoading(getContext(),"");
            OkHttpUtil.getInstance_dababy().addWalletAccount_Fr(
                    mobile1,
                    mobile2,
                    channel,
                    () -> {if (bindAccountListener_dababy !=null) bindAccountListener_dababy.onBindAccount_dababy(finalMobile);},
                    false
            );

            dismiss();

        });

        //手机号输入框焦点监听
        mobile1Et.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                mobile1View.setBackgroundResource(R.drawable.bg_input_blue_border);
                mobile1Et.setTextColor(getContext().getColor(R.color.main));
            }else {
                mobile1View.setBackgroundResource(R.drawable.bg_input_black);
                mobile1Et.setTextColor(getContext().getColor(R.color.white));
            }
        });

        //手机号输入框焦点监听
        mobile2Et.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                mobile2View.setBackgroundResource(R.drawable.bg_input_blue_border);
                mobile2Et.setTextColor(getContext().getColor(R.color.main));
            }else {
                mobile2View.setBackgroundResource(R.drawable.bg_input_black);
                mobile2Et.setTextColor(getContext().getColor(R.color.white));
            }
        });

    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_dababy().uploadEventPoint(page_dababy,"DIALOG_CLOSE_CLICK","");
    }

}
